package com.spc.car.data.service.project.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.spc.car.common.utils.StringUtils;
import com.spc.car.data.entity.project.Project;
import com.spc.car.data.mapper.project.ProjectMapper;
import com.spc.car.data.service.project.ProjectService;
import com.spc.car.data.service.sys.SysAttachmentService;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * <p>
 * 项目表 服务实现类
 * </p>
 *
 * @author 
 * @since 2020-03-25
 */
@Service
@AllArgsConstructor
public class ProjectServiceImpl extends ServiceImpl<ProjectMapper, Project> implements ProjectService {

    private static final String PROJECT_CODE = "CS";
    private static final String DATA_FORMAT = "yyyyMMdd";

    @Autowired
    SysAttachmentService attachmentService;


    /**
     * 获取最大项目编码
     * @return
     */
    private String getMaxProjectCode() {
        String pCode = baseMapper.getMaxCode();
        if (StringUtils.isNotEmpty(pCode)) {
            int length = pCode.length();
            String codeValues = pCode.substring(length - 3, length);
            return codeValues;
        }
        return "";
    }

    /**
     * 按照顺序自动生成顺从最大开始
     * @param equipmentNo 最大唯一编码
     * @return
     */
    private String getInputProjectCode(String equipmentNo) {
        String newEquipmentNo = "000";
        if (equipmentNo != null && !equipmentNo.isEmpty()) {
            int equipment = Integer.parseInt(equipmentNo) + 1;
            newEquipmentNo = String.format("%03d", equipment);
        }
        return newEquipmentNo;
    }

    @Override
    public String GenerateProjectCode() {
        StringBuffer biddingProjectCode = new StringBuffer();
        //设置日期格式
        SimpleDateFormat dateStyle = new SimpleDateFormat("yyyyMMdd");
        String date = dateStyle.format(new Date(System.currentTimeMillis()));
        int numberOfCode = count(new QueryWrapper<Project>()
                .like("project_code","PP"+date)
        );
        if (numberOfCode > 0){
            numberOfCode++;
            String endNumber = String.format("%03d", numberOfCode);
            biddingProjectCode = new StringBuffer("PP").append(date).append(endNumber);
        }else {
            biddingProjectCode = new StringBuffer("PP").append(date).append("001");
        }
        return String.valueOf(biddingProjectCode);
    }
}
